package org.eenie.hookui.ui

import android.app.Activity
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.text.HtmlCompat
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.*
import okhttp3.MediaType.Companion.toMediaType
import org.eenie.hookui.R
import org.eenie.hookui.databinding.ActivityPluginMainBinding
import org.eenie.hookui.utils.OkHttpUtils


class PluginMainActivity : AppCompatActivity() {

    private lateinit var mBinding: ActivityPluginMainBinding
    private val client = OkHttpClient.Builder()
        .build()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mBinding = ActivityPluginMainBinding.inflate(layoutInflater)
        setContentView(mBinding.root)



        mBinding.btnKill.setOnClickListener {
            val request = Request.Builder()
                .get()
                .url("https://www.hao123.com/")
                .build()
            MainScope().launch {
                val text = withContext(Dispatchers.IO) {
                    val response = client.newCall(request).execute()
                    response.body?.string()
                }

                mBinding.tvToken.text = text


            }
        }


        mBinding.btnPost.setOnClickListener {
            val request = Request.Builder()
                .url("https://www.wanandroid.com/user/login")
                .post(RequestBody.create("application/json".toMediaType(), "{\"username\":\"eenie\",\"password\":\"123456\"}"))
                .build()
            MainScope().launch {
                val text = withContext(Dispatchers.IO) {
                    val response = client.newCall(request).execute()
                    response.body?.string()
                }

                mBinding.tvToken.text = text
            }
        }


    }

}